<?php
	session_start();
	if (!isset($_SESSION['cart']))
		die('<p style="color:red;text-align:center;font-size:6px">Lỗi: Giỏ hàng trống!</p>');
?>
	<script type="text/javascript" src="js/jquery.js"></script>
	<script type="text/javascript">
		function checkComp()
		{
			if(document.getElementById("comp").value.match(/[a-zA-Z0-9]{3,30}/))
			{
				document.getElementById("chide").style.display="none"
			}
			else {
				document.getElementById("chide").style.display="inline-block"
			}
		}
		function checkEmail()
		{
			if(document.getElementById("email").value.match(/\b[a-zA-Z0-9]+@[a-zA-Z0-9.-]{3,5}\.[a-zA-Z]{2,4}\b/))
                {
                    document.getElementById("ehide").style.display="none";
                }
            else {
                    document.getElementById("ehide").style.display="inline-block";
                }
		}	
		function checkName()
		{
			if(document.getElementById("name").value.match(/[a-zA-Z0-9]{2,20}/))
				{
					document.getElementById("nhide").style.display="none"
				}
			else {
					document.getElementById("nhide").style.display="inline-block"
				}
		}
	</script>
	<div id="main2">
	<?php include("breadcrumb.php"); ?>
	<div id="main3">
		<div id="content">
			<br/>
			<h2 style = "text-align:center;">Chi Tiết Đơn Hàng</h2>
			<form>
				<table class="table_2">
				<tr>
					<td class="pay-label"><h>Họ và Tên:</h> </td>
					<td><input id="name" type = "text" class="txt" name = "name" onchange="checkName();"/>
						<span id="nhide" style="color:red;display:none"><i>Nhập Họ Tên</i></span>
					</td>
				</tr>
				<tr>
					<td class="pay-label"><h>Giớ́i Tính:</h> </td>
					<td><select name = "sex"><option value="">Choose...</option><option value="male">Nam</option><option value="female">Nữ</option></select></td>
				</tr>
				<tr>
					<td class="pay-label"><h>Điện thoại:</h> </td>
					<td><input type = "text" class="txt" name = "phone"></td>
				</tr>
				<tr>
					<td class="pay-label"><h>Email:</h> </td>
					<td><input id="email" type = "text" class="txt" name = "email" onchange="checkEmail();"/>
						<span id="ehide" style="color:red;display:none"><i>Nhập E-mail</i></span></td>
					</td>
				</tr>
				<tr>
					<td class="pay-label"><h>Công ty:</h> </td>
					<td><input id="comp" type = "text" class="txt" name = "company" onchange="checkComp();"/>
						<span id="chide" style="color:red;display:none"><i>Nhập công ty</i></span>
					</td>
				</tr>
				<tr>
					<td class="pay-label"><h>Địa Chỉ:</h> </td>
					<td><input type = "text" class="txt" style = "margin-right:50px;" name = "address"></td>
				</tr>
				<tr>
					<td class="pay-label"><h>Ngày giao hàng:</h> </td>
					<td>
						<input type = "text" size = "2" name = "day" />
						<input type = "text" size = "2" name = "month" />
						<input type = "text" size = "4" name = "year" /> <br/>
						<h6>*Ngày giao hàng phải sau ngày đặt hàng ít nhất là 3 ngày</h6>
					</td>
				</tr>
				</table>
<?php
					$items = $_SESSION['cart'];
					$ids = array_keys($items);
					$str = implode(",",$ids);			
					$sql = "SELECT * FROM product WHERE id in ($str)";
					$rs = mysql_query($sql);
					$tong = 0;
					if (!$rs)
					{
						echo '<p style="text-align:center;color:red;font-size:16px;">Lỗi: Giỏ hàng rỗng!</p>';
					}
					else
	{
?>

		<table class="table_1">
				<thead>
					<tr>
						<th>Mã SP</th>
						<th>Tên</th>
						<th>Đơn Giá</th>
						<th>Số Lượng</th>
						<th>Thành Tiền</th>
					</tr>
				</thead>


	<?php


					while($rw = mysql_fetch_array($rs))
					{
						$tong_temp = ($rw["price"])* ($items[$rw["id"]]);
						$tong = $tong + $tong_temp;
					?>					
				<tbody>
					<tr>
						<td><?php echo $rw["id"]; ?></td>
						<td><?php echo $rw["name"]; ?></td>
						<td><?php echo number_format($rw["price"],0,',','.') ?></td>
						<td><?php echo $items[$rw["id"]] ?></td>
						<td><?php echo number_format($tong_temp ,0,',','.')?></td>
		
					</tr>
					<?php
					 $i+=1;
					 }
					?>
					<tr>
						<td colspan = "4" style = "background-color:#F5F5F6;text-align:right" > <b>Số Tiền Cần Thanh Toán:</b> </td> 
						<td style = "color:red;background-color:#F5F5F6;"><?php echo number_format($tong ,0,',','.'); ?> </td>
					</tr>
				</tbody>
				</table>
<?php
		}
?>

				<br/>
				<div style = "text-align:center;">
				<input type="submit" name="addcontract" value="Đặt Hàng" style =" height:30px; width:80px;" /></input>
				<input type = "button" name = "Cancel" value = " Reset " style =" height:30px; width:80px;" Onclick = "func2()"></input>
				<br/>
			</form>
<?php
				if(isset($_REQUEST["addcontract"]))
				{
					$name = $_REQUEST["name"];
					$sex = $_REQUEST["sex"];
					$phone = $_REQUEST["phone"];
					$email = $_REQUEST["email"];
					$company = $_REQUEST["company"];
					$address = $_REQUEST["address"];
					$deliverdate = $_REQUEST["year"].'-'.$_REQUEST["month"].'-'.$_REQUEST["day"];
					$price = $tong;
					
					$sql="INSERT INTO contract(id,customer_name,sex,phone,email,company,address,price,deliver_date,status) 
					      VALUES (0,'".$name."','".$sex."','".$phone."','".$email."','".$company."','".$address."','".$price."','".$deliverdate."',0)";
					//echo "[$sql]";
					$rs=mysql_query($sql);
					$last_id = mysql_insert_id();
					//if($rs)echo "ok"; else echo "not ok";
					//get the last id with autoincrement
					
					$items = $_SESSION['cart'];
					$ids = array_keys($items);
					$str = implode(",",$ids);			
					$sql = "SELECT * FROM product WHERE id in ($str)";
					$rs = mysql_query($sql);
					while($rw = mysql_fetch_array($rs))
					{
						$tong_temp = ($rw["price"])* ($items[$rw["id"]]);
						$sql2 = "INSERT INTO product_deliver(id,id_product,id_contract,amount)
								 VALUES (0,'".$rw["id"]."','".$last_id."','".$items[$rw["id"]]."')";
						mysql_query($sql2);
					}
					echo "Đã Thành Công Yêu Cầu Đặt Hàng";
				}
				//if($rs) echo "ok2"; else echo "not ok2";
				
			?>
			</div>
		</div>
		<?php include("right.php"); ?>
	</div>